package kotan.view.table.editor;

import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import kotan.Kotan;
import kotan.datastore.api.Blob;
import kotan.model.EntityModel;
import kotan.util.BinaryUtil;

public class BlobEditDialog {

    public static void show(JComponent parentComponent, EntityModel model, int column) {
        JFileChooser dialog = new JFileChooser();
        dialog.setDialogTitle("File chooser");
        int result = dialog.showOpenDialog(parentComponent);
        if (result != JFileChooser.APPROVE_OPTION) return;
        File file = dialog.getSelectedFile();
        byte[] newData = BinaryUtil.read(file);
        if (newData == null) {
            JOptionPane.showMessageDialog(
                parentComponent,
                "Can't open :" + file.getAbsolutePath(),
                "File open error",
                JOptionPane.WARNING_MESSAGE);
            return;
        }
        Kotan.get().getDatastore().update(model, column, new Blob(newData));
    }
}
